<template>
  <ContentBase>
    <div class="row justify-content-md-center">
      <div class="col-3">
        <form @submit.prevent="register">
          <div class="mb-3">
            <label for="username" class="form-label">Username:</label>
            <input v-model="username" type="text" class="form-control" id="username" >
          </div>
          <div class="mb-3">
            <label for="password" class="form-label">Password:</label>
            <input v-model="password" type="password" class="form-control" id="password">
          </div>
          <div class="mb-3">
            <label for="confirmedPassword" class="form-label">confirmedPassword:</label>
            <input v-model="confirmedPassword" type="password" class="form-control" id="confirmedPassword">
          </div>
          <div class="error_message">{{error_message}}</div>
          <button type="submit" class="btn btn-primary">Submit</button>
        </form>
      </div>
    </div>
  </ContentBase>
</template>

<script>

import ContentBase from "@/components/ContentBase";
import {ref} from 'vue';
import $ from 'jquery';
import router from "@/router";
import {ElMessage} from "element-plus";

export default {
  name: "UserAccountRegisterView",
  components: {ContentBase},
  setup(){
    let username = ref("");
    let password = ref("");
    let confirmedPassword = ref("");
    let error_message = ref("");

    const register = () =>{
      $.ajax({
        url:"http://localhost:7012/user/account/register/",
        type:"post",
        data:{
          username:username.value,
          password:password.value,
          confirmedPassword:confirmedPassword.value,
        },
        success(resp){
          if (resp.message === "register_success"){
              ElMessage.success("注册成功")
              router.push({name:"Login"});
          }else {
            error_message.value = resp.message;
          }
        },
      });
    }

    return{
      username,
      password,
      confirmedPassword,
      error_message,
      register

    }

  }
}
</script>

<style scoped>
button{
  width: 100%;
}
.error_message{
  color: red;
}
</style>
